package com.risk.management.service.impl;

import com.risk.management.entity.Risk;
import com.risk.management.entity.RiskHistory;
import com.risk.management.mapper.RiskHistoryMapper;
import com.risk.management.service.RiskHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 风险历史记录服务实现类
 */
@Service
public class RiskHistoryServiceImpl implements RiskHistoryService {

    @Autowired
    private RiskHistoryMapper riskHistoryMapper;

    @Override
    public RiskHistory getRiskHistoryById(Long id) {
        return riskHistoryMapper.selectById(id);
    }

    @Override
    public List<RiskHistory> getRiskHistoriesByRiskId(Long riskId) {
        return riskHistoryMapper.selectByRiskId(riskId);
    }

    @Override
    @Transactional
    public boolean recordCreate(Risk risk, Long operatorId, String operatorUsername) {
        RiskHistory history = new RiskHistory();
        history.setRiskId(risk.getId());
        history.setOperationType(1); // 1表示创建
        history.setAfterStatus(risk.getStatus());
        history.setAfterOwnerId(risk.getOwnerId());
        history.setAfterLevel(risk.getLevel());
        history.setOperatorId(operatorId);
        history.setOperatorUsername(operatorUsername);
        history.setRemark("创建风险");
        history.setCreateTime(new Date());
        
        return addRiskHistory(history);
    }

    @Override
    @Transactional
    public boolean recordStatusUpdate(Risk risk, Integer beforeStatus, Integer afterStatus, 
                                     Long operatorId, String operatorUsername, String remark) {
        RiskHistory history = new RiskHistory();
        history.setRiskId(risk.getId());
        history.setOperationType(2); // 2表示更新状态
        history.setBeforeStatus(beforeStatus);
        history.setAfterStatus(afterStatus);
        history.setOperatorId(operatorId);
        history.setOperatorUsername(operatorUsername);
        history.setRemark(remark);
        history.setCreateTime(new Date());
        
        return addRiskHistory(history);
    }

    @Override
    @Transactional
    public boolean recordOwnerUpdate(Risk risk, Long beforeOwnerId, Long afterOwnerId, 
                                    Long operatorId, String operatorUsername, String remark) {
        RiskHistory history = new RiskHistory();
        history.setRiskId(risk.getId());
        history.setOperationType(3); // 3表示更新责任人
        history.setBeforeOwnerId(beforeOwnerId);
        history.setAfterOwnerId(afterOwnerId);
        history.setOperatorId(operatorId);
        history.setOperatorUsername(operatorUsername);
        history.setRemark(remark);
        history.setCreateTime(new Date());
        
        return addRiskHistory(history);
    }

    @Override
    @Transactional
    public boolean recordLevelUpdate(Risk risk, Integer beforeLevel, Integer afterLevel, 
                                    Long operatorId, String operatorUsername, String remark) {
        RiskHistory history = new RiskHistory();
        history.setRiskId(risk.getId());
        history.setOperationType(4); // 4表示更新风险级别
        history.setBeforeLevel(beforeLevel);
        history.setAfterLevel(afterLevel);
        history.setOperatorId(operatorId);
        history.setOperatorUsername(operatorUsername);
        history.setRemark(remark);
        history.setCreateTime(new Date());
        
        return addRiskHistory(history);
    }

    @Override
    @Transactional
    public boolean recordDelete(Risk risk, Long operatorId, String operatorUsername, String remark) {
        RiskHistory history = new RiskHistory();
        history.setRiskId(risk.getId());
        history.setOperationType(5); // 5表示删除
        history.setBeforeStatus(risk.getStatus());
        history.setBeforeOwnerId(risk.getOwnerId());
        history.setBeforeLevel(risk.getLevel());
        history.setOperatorId(operatorId);
        history.setOperatorUsername(operatorUsername);
        history.setRemark(remark);
        history.setCreateTime(new Date());
        
        return addRiskHistory(history);
    }

    @Override
    @Transactional
    public boolean addRiskHistory(RiskHistory riskHistory) {
        return riskHistoryMapper.insert(riskHistory) > 0;
    }
}